package com.zy.pro.shop.web;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.city.model.City;
import com.zy.pro.city.service.CityService;
import com.zy.pro.city.web.form.CitySearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.model.ShopUser;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.shop.service.ShopUserService;
import com.zy.pro.shop.web.form.ShopSearchForm;
import com.zy.utils.UploadHelper;
/**
 * 店铺管理控制器
 *
 */
@Controller
@RequestMapping("/control/shop/shopController")
public class ShopController extends BaseController {

	private static final Logger logger = Logger.getLogger(ShopController.class);
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopUserServiceImpl")
	private ShopUserService shopUserService;
	
	/**
	 * 城市管理服务类
	 */
	@Resource(name = "cityServiceImpl")
	private CityService cityService;
	
	/**
	 * 系统列出店铺管理
	 */
	@RequestMapping("/sysShopList")
	public String sysShopList(ShopSearchForm form,ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			if(form.getShopName() != null && form.getShopName() != ""){
				form.setShopName(form.getShopName().trim());
			}
			if(form.getShopContact() != null && form.getShopContact() != ""){
				form.setShopContact(form.getShopContact().trim());
			}
			
			form.getOrderby().put("shopCreateTime", "desc");
			
			//创建页面对象
			PageView<Shop> pageView = new PageView<Shop>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(shopService.getData(form));
			
			CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);
			map.addAttribute("cityList", cityList);
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/shop/shopController/sysShopList.do");
			//TODO 转发页面
			return "/control/system/shop/sysShopList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统编辑店铺状态
	 */
	@RequestMapping("/editStatus")
	public void editStatus(ShopSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			Shop s = shopService.find(form.getShopId());
			if(null!=s){
				Shop shop = new Shop();
				shop.setShopId(form.getShopId());
				shop.setShopStatus(form.getShopStatus());//1:正常,2:冻结,3:删除
				shopService.updateBySelective(shop);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	/**
	 * 系统显示添加店铺管理
	 */
	@RequestMapping("/dispalySysAdd")
	public String dispalySysAdd(Shop shop, ShopUser shopUser, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);
			
			map.addAttribute("operateStatus", "sysAdd");
			map.addAttribute("shop", shop);
			map.addAttribute("shopUser", shop.getShopUser());
			map.addAttribute("cityList", cityList);
			//TODO 转发页面
			return "/control/system/shop/sysEditShop";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统显示添加店铺管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(Shop shop, ShopUser shopUser, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			CitySearchForm cForm = new CitySearchForm();
			cForm.getOrderby().put("cityCreateTime", "desc");
			List<City> cityList = cityService.findAll(cForm);
			
			shop = shopService.find(shop.getShopId());
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("shop", shop);
			map.addAttribute("shopUser", shop.getShopUser());
			
			map.addAttribute("cityList", cityList);
			//TODO 转发页面
			return "/control/system/shop/sysEditShop";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统添加店铺管理
	 */
	@RequestMapping("/sysAdd")
	public String sysAdd(Shop shop, ModelMap map,HttpServletRequest request) {
		try {
			//TODO 设置shop  eg:主键信息
			if(shop.getShopLogo() != null && shop.getShopLogo().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = shop.getShopLogo().replaceAll("yibu", "shopLogo");
				UploadHelper.copyFile(shop.getShopLogo(), newPath, "yibu", request, logger);
				shop.setShopLogo(newPath);
				
			}
			//初始化店铺评价次数0
			shop.setShopCommentNum(0);
			//新建店铺默认给信誉度0
			//好评默认0
			//shop.setShopPraise(BigDecimal.valueOf(0));
			shopService.saveShopAndShopUser(shop);
//			shopService.save(shop);
			return sysShopList(new ShopSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑店铺管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(Shop shop, String oldShopLogo,ModelMap map, HttpServletRequest request) {
		try {
			//TODO:处理店铺管理
			
			if(shop.getShopLogo() != null && shop.getShopLogo().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = shop.getShopLogo().replaceAll("yibu", "shopLogo");
				UploadHelper.copyFile(shop.getShopLogo(), newPath, "yibu", request, logger);
				shop.setShopLogo(newPath);
				
				//删除旧Logo
				//UploadHelper.deletePic(oldShopLogo, "shopLogo", request, logger);
			}
//			shopService.updateBySelective(shop);
			shopService.updateShop(shop);
			return sysShopList(new ShopSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	
	/**
	 * 列出店铺管理
	 *//*
	@RequestMapping("/list")
	public String list(ShopSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			//创建页面对象
			PageView<Shop> pageView = new PageView<Shop>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(shopService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示添加店铺管理
	 *//*
	@RequestMapping("/displayAdd")
	public String displayAdd(Shop shop, ShopUser shopUser, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			shopService.save(shop);
			shopUserService.save(shopUser);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 添加店铺管理
	 *//*
	@RequestMapping("/add")
	public String add(Shop shop, ModelMap map) {
		try {
			//TODO 设置shop  eg:主键信息
			
			shopService.save(shop);
			return list(new ShopSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示编辑店铺管理
	 *//*
	@RequestMapping("/displayEdit")
	public String displayEdit(ShopSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Shop shop = shopService.find(form.getId());
			//eg:map.addAttribute("entity", shop);
			Shop shop = shopService.find(form.getShopId());
			map.addAttribute("shop", shop);
			
			//TODO 转发页面
			return "/control/shop/editShop";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 编辑店铺管理
	 *//*
	@RequestMapping("/edit")
	public String edit(Shop shop, String oldShopLogo,ModelMap map, HttpServletRequest request) {
		try {
			//TODO:处理店铺管理
			
			if(shop.getShopLogo() != null && shop.getShopLogo().indexOf("yibu") != -1){
				//将异步上传的Logo拷贝到正式路径下面
				String newPath = shop.getShopLogo().replaceAll("yibu", "shopLogo");
				UploadHelper.copyFile(shop.getShopLogo(), newPath, "yibu", request, logger);
				shop.setShopLogo(newPath);
				
				//删除旧Logo
				//UploadHelper.deletePic(oldShopLogo, "shopLogo", request, logger);
			}
			shopService.updateBySelective(shop);
			map.addAttribute("shop", shop);
			return "/control/shop/editShop";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	*//**
	 * 删除单个店铺管理
	 *//*
	@RequestMapping("/deleteshop")
	public String delete(ShopSearchForm form, ModelMap map) {
		try {
			//TODO  eg:shopService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 批量删除店铺管理
	 *//*
	@RequestMapping("/batchDelete")
	public String deletes(ShopSearchForm form, ModelMap map) {
		try {
//			TODO  eg:shopService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}*/
}
